<?php
namespace app\controllers;
use yii;
use yii\base\Controller;
use app\models\User;
class LoginController extends Controller
{
    /*登录 页面*/
    public function actionIndex()
    {
        $this->layout='login';
        return $this->render('login');
    }

    /*登录操作*/
    private function Login($name)

    {
        $identity = User::findOne(['name' => $name]);
        // 登录用户
        Yii::$app->user->login($identity);
    }

    /*登录验证*/
    public function actionCheckLogin()
    {
        $request=Yii::$app->request;
        $username=$request->post('username');
        $password=$request->post('password');
        $password=md5($password);
        $user = User::find()
            ->where(['name' => $username])
            ->one();
        if(!is_null($user) AND $password==$user['pass'])
        {
            $this->Login($username);
            header('Location:/');
//            return json_encode(array('status'=>'success'));
        }
        else
        {
            header('Location:/login');
//            return json_encode(array('status'=>'error'));
        }
    }

    /*注销操作*/
    public function actionLogout()
    {
        Yii::$app->user->logout();
        header('Location:/login');
    }
}